package com.own.component.store.local.method;

import com.own.component.store.core.method.StoreAtomic;
import com.own.component.store.core.property.LocalStoreProperty;
import com.own.component.store.local.method.scheduler.StoreAtomicScheduler;
import com.own.component.store.local.method.state.StoreState;
import lombok.extern.slf4j.Slf4j;

import java.time.Duration;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * DefaultLocalStoreAtomic
 *
 * @author chenxueli
 * @date 2023-06-28 14:51:18
 */
@Slf4j
public class DefaultLocalStoreAtomic extends DefaultBaseStoreMethod<AtomicInteger> implements StoreAtomic {

    public DefaultLocalStoreAtomic() {
        super(new StoreState<>());
    }

    public DefaultLocalStoreAtomic(long delay) {
        super(new StoreState<>(), StoreAtomicScheduler.INSTANCE, delay);
    }

    public DefaultLocalStoreAtomic(LocalStoreProperty properties) {
        super(new StoreState<>(), StoreAtomicScheduler.INSTANCE, properties);
    }

    /**
     * 获取原子值
     *
     * @param name 保存的name
     * @return 值
     */
    @Override
    public synchronized long get(String name) {
        var key = buildKey(name);
        var value = getStoreState().get(key);
        if (value == null) {
            return 0;
        }
        return value.get();
    }

    /**
     * 获取值
     *
     * @param key      key
     * @param duration 超时时间
     * @return 值
     */
    @Override
    public long get(String key, Duration duration) {
        var newKey = buildKey(key);
        var value = getStoreState().get(newKey);
        getStoreState().set(newKey, value, duration);
        if (value == null) {
            return 0;
        }
        return value.get();
    }

    /**
     * 获取原子值
     *
     * @param name 保存的name
     * @return 值
     */
    @Override
    public long getAndIncrement(String name) {
        var key = buildKey(name);
        var value = getStoreState().get(key);
        if (value == null) {
            var atomic = new AtomicInteger(0);
            getStoreState().set(key, atomic);
            return atomic.getAndIncrement();
        }
        return value.getAndIncrement();
    }

    /**
     * 获取值
     *
     * @param key      key
     * @param duration 超时时间
     * @return 值
     */
    @Override
    public long getAndIncrement(String key, Duration duration) {
        var newKey = buildKey(key);
        var value = getStoreState().get(newKey);
        if (value == null) {
            var atomic = new AtomicInteger(0);
            getStoreState().set(newKey, atomic, duration);
            return atomic.getAndIncrement();
        }
        getStoreState().expire(newKey, duration);
        return value.getAndIncrement();
    }

    /**
     * 获取原子值
     *
     * @param name 保存的name
     * @return 值
     */
    @Override
    public long incrementAndGet(String name) {
        var key = buildKey(name);
        var value = getStoreState().get(key);
        if (value == null) {
            var atomic = new AtomicInteger(0);
            getStoreState().set(key, atomic);
            return atomic.incrementAndGet();
        }
        return value.incrementAndGet();
    }

    /**
     * 获取值
     *
     * @param key      key
     * @param duration 超时时间
     * @return 值
     */
    @Override
    public long incrementAndGet(String key, Duration duration) {
        var newKey = buildKey(key);
        var value = getStoreState().get(newKey);
        if (value == null) {
            var atomic = new AtomicInteger(0);
            getStoreState().set(newKey, atomic, duration);
            return atomic.incrementAndGet();
        }
        getStoreState().expire(newKey, duration);
        return value.incrementAndGet();
    }

    /**
     * 获取所有的key
     *
     * @return 所有的key
     */
    @Override
    public Set<String> keySet() {
        return getStoreState().keySet();
    }

}
